### Protocol Analysis

The repository implements the **Crystal** protocol, which is a synchronous, reliable, and low-latency data collection protocol for wireless sensor networks. The file `baloo-crystal.c` confirms this is a "Re-implementation of the Crystal protocol".

Crystal operates in rounds or epochs. In each epoch:
1.  A sink node initiates communication by broadcasting a packet.
2.  This broadcast is performed using a **Glossy flood**, a concurrent transmission primitive where nodes that receive a packet immediately retransmit it. This ensures fast and reliable dissemination of the packet across the entire network.
3.  Other nodes in the network, upon receiving the sink's packet, can transmit their own data back to the sink in subsequent time slots.

The protocol is a form of **scheduled flooding** and **Time Division Multiple Access (TDMA)**, designed for high-reliability data gathering applications.

### Tunable Parameter Analysis

Here is the analysis of how `tx_power` and `n_tx_max` affect the protocol's performance.

#### 1. `tx_power`

This parameter controls the radio's transmission power level. The file `custom.h` notes that the value corresponds to a level defined in `TxPowerSettings.txt`.

*   **Effect on PRR (Packet Reception Rate):**
    *   **Increasing `tx_power` generally increases PRR.** A higher power output results in a stronger signal at the receiver, improving the Signal-to-Noise Ratio (SNR). This makes the transmission more resilient to background noise and interference, reducing bit errors and leading to more successful packet receptions. This is especially effective for nodes at the edge of communication range.
    *   However, an excessively high `tx_power` can cause interference to other nodes in the network or nearby networks, potentially degrading the overall network performance. It can also saturate the receivers of very close nodes.

*   **Effect on Energy Consumption:**
    *   **Increasing `tx_power` significantly increases energy consumption.** The power amplifier in the radio is one of the most energy-hungry components. The relationship is non-linear, meaning a small boost in output power can lead to a large increase in current draw, draining the battery faster.

*   **Effect on Latency:**
    *   `tx_power` has an indirect effect on latency. By improving the PRR, it increases the probability that a packet is delivered successfully within a single Glossy flood. This enhances reliability within the fixed-duration slot, reducing the likelihood of packet loss that would necessitate retransmission in a future epoch, thereby lowering the effective end-to-end latency for a data packet.

#### 2. `n_tx_max`

This parameter, defined in `custom.h` as the "Number of retransmissions in a Glossy flood," dictates how many times a packet is re-broadcasted by the network during a single Glossy phase.

*   **Effect on PRR:**
    *   **Increasing `n_tx_max` significantly increases PRR.** It introduces both time and spatial diversity. If a transmission is corrupted by transient noise, a subsequent retransmission might succeed (time diversity). As more nodes receive and retransmit the packet, it finds more paths through the network, overcoming local dead spots or link failures (spatial diversity). This makes the flood extremely robust.

*   **Effect on Energy Consumption:**
    *   **Increasing `n_tx_max` directly increases energy consumption.** Every node participating in the flood will transmit the packet more times. This means the radio is active in the high-power TX state for a longer duration, leading to a roughly linear increase in energy usage for that communication phase.

*   **Effect on Latency:**
    *   **Increasing `n_tx_max` increases latency per communication phase.** The duration of a Glossy flood is directly proportional to the number of transmissions. More retransmissions mean each slot in the Crystal epoch becomes longer, thus increasing the total epoch duration and the latency for a single data collection round.

### Optimization Hints

There is a fundamental trade-off between reliability (PRR), energy, and latency.

*   **For Maximum Reliability:** Increase both `n_tx_max` and `tx_power`. This is the most energy-intensive configuration but provides the highest chance of packet delivery.
*   **For Minimum Energy Consumption:** Use the lowest possible `tx_power` and `n_tx_max` that still meet your application's PRR requirements.
*   **For Minimum Latency:** Use a low `n_tx_max` to keep the Glossy floods short. You may need to compensate with a higher `tx_power` to maintain an acceptable PRR.

**Recommended Strategy:**
1.  Start with a moderate `tx_power` and a low `n_tx_max` (e.g., 3).
2.  If the PRR is insufficient, first try increasing `n_tx_max`. Leveraging network diversity is often more energy-efficient for the entire network than simply increasing `tx_power`.
3.  If increasing `n_tx_max` leads to unacceptable latency or is still not reliable enough, increase `tx_power` in small steps.
4.  The optimal point is the lowest combination of `tx_power` and `n_tx_max` that consistently achieves your target PRR across the specific deployment environment.